home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / util / rexx / rexxmode10.lha / rexx-debug.el < prev    next >
Lisp/Scheme  |  1993-03-20  |  10KB  |  308 lines

  1. ;;;
  2. ;;; FILE
  3. ;;;    rexx-debug.el    V1.0
  4. ;;;
  5. ;;;    Copyright (C) 1993 by Anders Lindgren.
  6. ;;;
  7. ;;;    This file is NOT part of GNU Emacs (yet).
  8. ;;;
  9. ;;; DISTRIBUTION
  10. ;;;    REXX-debug is free software; you can redistribute it and/or modify
  11. ;;;    it under the terms of the GNU General Public License as published 
  12. ;;;    by the Free Software Foundation; either version 1, or (at your 
  13. ;;;    option) any later version.
  14. ;;;
  15. ;;;    GNU Emacs is distributed in the hope that it will be useful,
  16. ;;;    but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;;;    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. ;;;    GNU General Public License for more details.
  19. ;;;
  20. ;;;    You should have received a copy of the GNU General Public
  21. ;;;    License along with GNU Emacs; see the file COPYING.  If not,
  22. ;;;    write to the Free Software Foundation, 675 Mass Ave, Cambridge,
  23. ;;;    MA 02139, USA.
  24. ;;;
  25. ;;;
  26. ;;; AUTHOR
  27. ;;;    Anders Lindgren, d91ali@csd.uu.se
  28. ;;;
  29. ;;; USAGE
  30. ;;;    To use this program, call "rexx-debug", enter a filename,
  31. ;;;    or press return if you would like to debug the current file.
  32. ;;;    Enter the arguments to the rexx program and press return.
  33. ;;;    The output from the program and debuginformation will be
  34. ;;;    shown in "*rexx-<name>*".
  35. ;;;
  36. ;;;    Very simple REXX source level debugger. Currently, the only thing
  37. ;;;    it reallt does is reads the debug info and places the arrow
  38. ;;;    on the correct line.
  39. ;;;
  40. ;;;    To use this program, the rexx script must be run in interactive
  41. ;;;    debug mode. This is controlled by the '?' trace flag. You can
  42. ;;;    for example place this line in the beginning of the script:
  43. ;;;        trace ?r
  44. ;;;
  45. ;;; HISTORY
  46. ;;;    93-01-11 ALi Start of codeing based on comint-gdb
  47. ;;;    93-01-15     Works very well, thank you!
  48. ;;;    93-03-16     rxdb-command-name removed as local function.
  49. ;;;
  50.  
  51. (require 'comint)
  52. (provide 'rexx-debug)
  53.  
  54. (defvar rxdb-lineno-regexp "^ +[0-9]+ +\\*\\-\\* "
  55.   "A regexp to recognize a linenumber in the rexx debugger output stream.")
  56.  
  57. (defvar rxdb-prompt-pattern "^>\\+> "
  58.   "A regexp to recognize the rexx debugger prompt.")
  59.  
  60. (defvar rxdb-command-name "rx"
  61.   "Pathname for REXX interpreter.")
  62.  
  63. (defvar rxdb-mode-map nil
  64.   "Keymap for rexx-debug-mode.")
  65.  
  66. (if rxdb-mode-map
  67.     nil
  68.   (setq rxdb-mode-map (full-copy-sparse-keymap comint-mode-map))
  69.   (define-key rxdb-mode-map "\C-l" 'rxdb-refresh))
  70.  
  71. (defun rxdb-mode ()
  72.   "Major mode for interacting with an inferior rexx process.
  73.  
  74. Commands:
  75.  
  76. \\{rxdb-mode-map}
  77.  
  78. Variable:
  79.     rxdb-command-name  contains the name of the REXX interpretator.
  80.                Default is \"RX\", which is used by ARexx.
  81.  
  82. Customisation: Entry to this mode runs the hooks comint-mode-hook and
  83. rxdb-mode-hook (in that order).
  84.  
  85. For example:
  86. (setq rxdb-mode-hook '(lambda ()
  87.                 (setq rxdb-command-name \"/usr/local/bin/rexx\")
  88.             (define-local-key \"key\" 'favorite-command)
  89.             ))
  90.  
  91. will set the command so it can be used in many UNIX environments."
  92.   (interactive)
  93.   (comint-mode)
  94.   (setq major-mode 'rxdb-mode)
  95.   (setq mode-name "Inferior REXX")
  96.   (setq mode-line-process '(": %s"))
  97.   (use-local-map rxdb-mode-map)
  98.   (setq comint-prompt-regexp rxdb-prompt-pattern)
  99.   (make-local-variable 'rxdb-last-frame)
  100.   (setq rxdb-last-frame nil)
  101.   (make-local-variable 'rxdb-last-frame-displayed-p)
  102.   (setq rxdb-last-frame-displayed-p t)
  103.   (make-local-variable 'rxdb-delete-prompt-marker)
  104.   (setq rxdb-delete-prompt-marker nil)
  105.   (run-hooks 'rxdb-mode-hook))
  106.  
  107. (defun rexx-debug (path args)
  108.   "Run a rexx program FILE in buffer *rexx-FILE*.
  109. The directory containing FILE becomes the initial working directory
  110. and source-file directory.
  111. \(Type \\[describe-mode] in the process buffer for a list of commands.)"
  112.   (interactive "FDebug file (return for current buffer): \nsArguments:")
  113.   (setq path (expand-file-name path))
  114.   (let* ((file (file-name-nondirectory path))
  115.      (rxdb-buffer (concat "*rexx-" file "*"))
  116.      (rxdb-window (get-buffer-window rxdb-buffer)))
  117.     (if rxdb-window 
  118.     (select-window rxdb-window)
  119.     (switch-to-buffer rxdb-buffer))
  120.     (if (comint-check-proc rxdb-buffer) nil
  121.       (setq default-directory (file-name-directory path))
  122.       (or (bolp) (newline))
  123.       (insert "Current directory is " default-directory "\n")
  124.       (make-comint (concat "rexx-" file) rxdb-command-name nil
  125.            (concat file " " args))
  126.       (rxdb-mode)
  127.       (set-process-filter (get-buffer-process (current-buffer)) 'rxdb-filter)
  128.       (set-process-sentinel (get-buffer-process (current-buffer)) 
  129.                 'rxdb-sentinel))
  130.     (rxdb-set-buffer path)))
  131.  
  132. (defun rxdb-set-buffer (&optional path)
  133.   (cond ((eq major-mode 'rxdb-mode)
  134.      (setq current-rxdb-buffer (current-buffer))
  135.      (if path
  136.          (setq current-rxdb-file path)))))
  137.  
  138. ;; This function is responsible for inserting output from the rexx 
  139. ;; debugger into the buffer. It records the linenumber for the
  140. ;; placement of the arrow.
  141. (defun rxdb-filter (proc string)
  142.   (let ((inhibit-quit t))
  143.     (rxdb-filter-accumulate-marker proc string)))
  144.  
  145. (defun rxdb-filter-accumulate-marker (proc string)
  146.   (let ((end t))
  147.     (while end
  148.       (setq end (string-match "\012" string))
  149.       (if end
  150.       (progn
  151.         (if (string-match rxdb-lineno-regexp string)
  152.         (progn
  153.           (setq rxdb-last-frame 
  154.             (string-to-int (substring string 0 
  155.                           (string-match 
  156.                            "\\*\\-\\*" string 1))))
  157.           (setq rxdb-last-frame-displayed-p nil)))
  158.         (rxdb-filter-insert proc (substring string 0 (1+ end)))
  159.         (setq string (substring string (1+ end))))))
  160.     (if (equal string "") nil
  161.       (rxdb-filter-insert proc string))))
  162.  
  163. (defun rxdb-filter-insert (proc string)
  164.   (let ((moving (= (point) (process-mark proc)))
  165.     (output-after-point (< (point) (process-mark proc)))
  166.     (old-buffer (current-buffer))
  167.     start)
  168.     (set-buffer (process-buffer proc))
  169.     (unwind-protect
  170.     (save-excursion
  171.       ;; Insert the text, moving the process-marker.
  172.       (goto-char (process-mark proc))
  173.       (setq start (point))
  174.       (insert string)
  175.       (set-marker (process-mark proc) (point))
  176.       (rxdb-maybe-delete-prompt)
  177.       ;; Check for new linenumber.
  178.       (rxdb-display-frame
  179.        ;; Don't display the specified file
  180.        ;; unless (1) point is at or after the position where output appears
  181.        ;; and (2) this buffer is on the screen.
  182.        (or output-after-point
  183.            (not (get-buffer-window (current-buffer))))
  184.        ;; Display a file only when a new linenumber appears.
  185.        t))
  186.       (set-buffer old-buffer))
  187.     (if moving (goto-char (process-mark proc)))))
  188.  
  189. (defun rxdb-sentinel (proc msg)
  190.   (cond ((null (buffer-name (process-buffer proc)))
  191.      ;; buffer killed
  192.      ;; Stop displaying an arrow in a source file.
  193.      (setq overlay-arrow-position nil)
  194.      (set-process-buffer proc nil))
  195.     ((memq (process-status proc) '(signal exit))
  196.      ;; Stop displaying an arrow in a source file.
  197.      (setq overlay-arrow-position nil)
  198.      ;; Fix the mode line.
  199.      (setq mode-line-process
  200.            (concat ": "
  201.                (symbol-name (process-status proc))))
  202.      (let* ((obuf (current-buffer)))
  203.        ;; save-excursion isn't the right thing if
  204.        ;;  process-buffer is current-buffer
  205.        (unwind-protect
  206.            (progn
  207.          ;; Write something in *compilation* and hack its mode line,
  208.          (set-buffer (process-buffer proc))
  209.          ;; Force mode line redisplay soon
  210.          (set-buffer-modified-p (buffer-modified-p))
  211.          (if (eobp)
  212.              (insert ?\n mode-name " " msg)
  213.            (save-excursion
  214.              (goto-char (point-max))
  215.              (insert ?\n mode-name " " msg)))
  216.          ;; If buffer and mode line will show that the process
  217.          ;; is dead, we can delete it now.  Otherwise it
  218.          ;; will stay around until M-x list-processes.
  219.          (delete-process proc))
  220.          ;; Restore old buffer, but don't restore old point
  221.          ;; if obuf is the rxdb buffer.
  222.          (set-buffer obuf))))))
  223.  
  224.  
  225. (defun rxdb-refresh ()
  226.   "Fix up a possibly garbled display, and redraw the arrow."
  227.   (interactive)
  228.   (redraw-display)
  229.   (rxdb-display-frame))
  230.  
  231. (defun rxdb-display-frame (&optional nodisplay noauto)
  232.   "Display the last line executed in another window."
  233.   (interactive)
  234.   (rxdb-set-buffer)
  235.   (and rxdb-last-frame (not nodisplay)
  236.        (or (not rxdb-last-frame-displayed-p) (not noauto))
  237.        (progn (rxdb-display-line current-rxdb-file rxdb-last-frame)
  238.           (setq rxdb-last-frame-displayed-p t))))
  239.  
  240. ;; Make sure the file named TRUE-FILE is in a buffer that appears on the screen
  241. ;; and that its line LINE is visible.
  242. ;; Put the overlay-arrow on the line LINE in that buffer.
  243.  
  244. (defun rxdb-display-line (true-file line)
  245.   (let* ((buffer (find-file-noselect true-file))
  246.      (window (display-buffer buffer t))
  247.      (pos))
  248.     (save-excursion
  249.       (set-buffer buffer)
  250.       (save-restriction
  251.     (widen)
  252.     (goto-line line)
  253.     (setq pos (point))
  254.     (setq overlay-arrow-string "=>")
  255.     (or overlay-arrow-position
  256.         (setq overlay-arrow-position (make-marker)))
  257.     (set-marker overlay-arrow-position (point) (current-buffer)))
  258.       (cond ((or (< pos (point-min)) (> pos (point-max)))
  259.          (widen)
  260.          (goto-char pos))))
  261.     (set-window-point window overlay-arrow-position)))
  262.  
  263.  
  264. (defun rxdb-call (command)
  265.   "Invoke rexx debug COMMAND displaying source in other window."
  266.   (interactive)
  267.   (goto-char (point-max))
  268.   (setq rxdb-delete-prompt-marker (point-marker))
  269.   (rxdb-set-buffer)
  270.   (send-string (get-buffer-process current-rxdb-buffer)
  271.            (concat command "\n")))
  272.  
  273. (defun rxdb-maybe-delete-prompt ()
  274.   (if (and rxdb-delete-prompt-marker
  275.        (> (point-max) (marker-position rxdb-delete-prompt-marker)))
  276.       (let (start)
  277.     (goto-char rxdb-delete-prompt-marker)
  278.     (setq start (point))
  279.     (beginning-of-line)
  280.     (delete-region (point) start)
  281.     (setq rxdb-delete-prompt-marker nil))))
  282.  
  283.  
  284. (defvar rxdb-commands nil
  285.   "List of strings or functions used by send-rxdb-command.
  286. It is for customization by you.")
  287.  
  288. (defun send-rxdb-command (arg)
  289.   "This command reads the core-address where the cursor is positioned.  It
  290.  then inserts this ADDR at the end of the rxdb buffer.  A numeric arg
  291.  selects the ARG'th member COMMAND of the list rxdb-commands.  If
  292.  COMMAND is a string, (format COMMAND ADDR) is inserted, otherwise
  293.  (funcall COMMAND ADDR) is inserted.  eg. \"p (rtx)%s->fld[0].rtint\"
  294.  is a possible string to be a member of rxdb-commands.  "
  295.   (interactive "P")
  296.   (let (comm addr)
  297.     (if arg (setq comm (nth arg rxdb-commands)))
  298.     (setq addr (rxdb-read-address))
  299.     (if (eq (current-buffer) current-rxdb-buffer)
  300.     (set-mark (point)))
  301.     (cond (comm
  302.        (setq comm
  303.          (if (stringp comm) (format comm addr) (funcall comm addr))))
  304.       (t (setq comm addr)))
  305.     (switch-to-buffer current-rxdb-buffer)
  306.     (goto-char (dot-max))
  307.     (insert-string comm)))
  308.